from tests.dayu_business_background.game import activityapi as ac
from common.config import *
import allure
import pytest
import json
import datetime
from common.opmysql import *
import time

#锦鲤榜
game="jinlibang"

# 多场次的报名+抽奖,取excel中第n行的数据,场次奖品的分级数
def choujiang(env,activityId,n,a,phoneopenid,phone,exchangeType):
    r = ac.TestSession().sessionlist(env, activityId)
    data = getdata(env, game, n)
    sessionIdlist = []
    allsession=r.json()['data']
    for i in range(0, len(allsession)):
        status = allsession[i]['status']
        sessionId = allsession[i]['id']
        prizeIdlist = []
        # print ("场次id",sessionId)
        data['independentPrizePoolVO']['sessionList'][i]['id'] = sessionId
        # 1级奖和多级奖的更新sessionid和获取prizeid
        if a>0:
            for x in range(a+1):
                data['independentPrizePoolVO']['sessionList'][i]['prizeList'][x]['sessionId'] = sessionId
                prizeId = data['independentPrizePoolVO']['sessionList'][i]['prizeList'][x]['id']
                prizeIdlist.append(prizeId)
        else:
            data['independentPrizePoolVO']['sessionList'][i]['prizeList'][a]['sessionId'] = sessionId
            prizeId = data['independentPrizePoolVO']['sessionList'][i]['prizeList'][a]['id']
        # 修改活动
        qqqq=ac.TestActivity().updateactivity(env, data)
        ac.TestActivity().enableOrDisableGame(env, activityId, 1)
        # print ("修改活动的数据",data)
        print ("修改活动",qqqq.json())
        delwhitelist(env, activityId)
        print ("改完后活动状态",status)
        if n==5:
            # 上传白名单
            r = ac.TestActivity().uploadFile(env, activityId)
            print("上传白名单", r.json())
            # 白名单列表
            rx = ac.TestSession().Whitelist(env, activityId)
            print("白名单列表", rx.json())
        while status==1:
            r1=ac.TestSession().sessionlist(env, activityId)
            status = r1.json()['data'][i]['status']
            # print ("应该走这里了啊",status,i)
            time.sleep(1)
            stubId = str(random.randint(10000, 99999))
            if status==2:
                if n ==5:
                    print("1个场次，不需要报名")
                else:
                    r2=ac.TestUser().signUp(env, activityId, sessionId, phoneopenid, phone, exchangeType, 1,stubId, 1)
                    print ("报名",r2.json())
                    if r2.json()['msg']=='只有会员用户才能参与':
                        print ("不是会员")
                    if r2.json()['msg'] == '报名失败,很抱歉，积分不足':
                        print("积分不足")
                    else:
                        pytest.assume(r2.json()['msg']=='success')
                        while status==2:
                            r3 = ac.TestSession().sessionlist(env, activityId)
                            status = r3.json()['data'][i]['status']
                            time.sleep(1)
                            if status==3:
                                if a > 0:
                                    for y in range(len(prizeIdlist)):
                                        r4 = ac.TestSession().sessionprocess(env, activityId, sessionId, 1, 1, 1)
                                        print("抽奖", r4.json())
                                        pytest.assume(r4.json()['msg'] == 'success')
                                        print("场次id和奖品id",sessionId,prizeIdlist[y])
                                        ac.TestSession().sessionprocess(env, activityId, sessionId, 2, prizeIdlist[y], 99)
                                        ac.TestSession().sessionprocess(env, activityId, sessionId, 3, prizeIdlist[y], 1)
                                else:
                                    r4 = ac.TestSession().sessionprocess(env, activityId, sessionId, 1, 1, 1)
                                    print("抽奖", r4.json())
                                    print("场次id和奖品id", sessionId, prizeId)
                                    pytest.assume(r4.json()['msg'] == 'success')
                                    ac.TestSession().sessionprocess(env, activityId, sessionId, 2, prizeId, 99)
                                    ac.TestSession().sessionprocess(env, activityId, sessionId, 3, prizeId, 1)
        sessionIdlist.append(sessionId)
    return sessionIdlist

# 删除场次
def delsession(env,activityId):
    r = ac.TestSession().sessionlist(env, activityId)
    print ("查询场次",r.json())
    # 删除所有场次
    if len(r.json()['data']) > 0:
        for i in range(0, len(r.json()['data'])):
            id = r.json()['data'][i]['id']
            # print ("场次id",id)
            ac.TestSession().delSession(env, activityId, id)
            i += 1
    r1 = ac.TestSession().sessionlist(env, activityId)
    print ("清空场次",r1.json())

# 删除白名单
def delwhitelist(env,activityId):
    activity=str(activityId)
    sql = 'SELECT id FROM platform_game.activity_participate_whitelist WHERE activity_id="' + activity + '" '
    whiteids = OperationDb(env).select_all(sql)
    id = ','.join(whiteids.split())
    arr = id.split(',')
    # print("白名单",arr)
    if len(whiteids)>2:
        r=ac.TestSession().delWhitelist(env,arr)
        print ("删白名单")
    else:
        print("白名单空的，不用删")

class Test_jinlibang():
    @allure.testcase('场次时间未正序排')
    def test_Sessionwrongtime(self,env):
        activityId = getconf(env, game)
        delsession(env, activityId)
        #新增时间未正序排的场次
        startTime,prossingStartTime,endTime= gettime(-60),gettime(5),gettime(-40)
        # print(startTime,prossingStartTime,endTime)
        r = ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        print("新增场次", r.json())
        pytest.assume(r.json()['msg'] == '时间选择不对')
        time.sleep(3)

    @allure.testcase('场次时间交叉提示')
    def test_Sessionmixtime(self,env):
        activityId = getconf(env, game)
        delsession(env, activityId)
        #新增场次1
        startTime,prossingStartTime,endTime=gettime(10),gettime(20),gettime(30)
        r = ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        print("新增场次", r.json())
        pytest.assume(r.json()['msg'] == 'success')
        # 新增场次2，时间与场次1时间有交叉
        startTime1,prossingStartTime1,endTime1 = gettime(10),gettime(20),gettime(30)
        r1 = ac.TestSession().saveSession(env, activityId, startTime1, prossingStartTime1, endTime1)
        msg= (r1.json()['msg'])
        pytest.assume(msg=='时间区间有交叉')

    @allure.testcase('场次未开始时，报名/抽奖提示')
    def test_unstartSession(self,env):
        # 修改活动
        data = getdata(env, game, 1)
        activityId = getconf(env, game)
        ac.TestActivity().updateactivity(env, data)
        ac.TestActivity().enableOrDisableGame(env, activityId, 1)
        delsession(env, activityId)
        phone = getphone()
        #新增1分钟后的场次
        startTime,prossingStartTime,endTime=gettime(120),gettime(130),gettime(140)
        r=ac.TestSession().saveSession(env, activityId,startTime,prossingStartTime,endTime)
        # print(r.json())
        pytest.assume(r.json()['msg']=='success')
        #获取当前有效场次
        r1=ac.TestSession().getEffective(env,activityId)
        id=int(r1.json()['data']['id'])
        # print(r1.json())
        pytest.assume(int(r1.json()['data']['status'])==1)
        # 场次未开始时调用报名接口
        stubId = str(random.randint(10000, 99999))
        r3=ac.TestUser().signUp(env,activityId,id,"phone",phone,5,1,stubId,1)
        print("场次未开始调报名接口",r3.json())
        pytest.assume(r3.json()['msg']=='本场抽奖还未开始，提交失败，耐心等待哦')
        # 场次未开始时调用手动抽奖
        r5=ac.TestSession().sessionprocess(env,activityId,id,1,1,1)
        # print ("开始抽奖，不到时间",r5.json())
        pytest.assume(r5.json()['msg'] == '不在开始抽奖时间范围内')

    @allure.testcase('白名单，独立奖池，1级奖，手动开奖，全局中奖限制1')
    # 非会员 / 白名单，前端输入，手机号关键字，手工录入
    def test_onelevprizelimit(self,env):
        activityId = getconf(env, game)
        delsession(env, activityId)
        #取第1条数据；1级奖
        n,a=1,0
        # 新增场次
        startTime, prossingStartTime, endTime = gettime(60), gettime(80), gettime(100)
        startTime1, prossingStartTime1, endTime1 = gettime(101), gettime(121), gettime(141)
        ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        ac.TestSession().saveSession(env, activityId, startTime1, prossingStartTime1, endTime1)
        phone = getphone()
        sessionIdlist=choujiang(env,activityId,n,a,"phone",phone,5)
        print ("全局限制第一次中奖",ac.TestActivity().listlottyName(env, activityId, sessionIdlist[0], 2).json()['data'])
        time.sleep(10)
        print ("全局限制第二次不中奖",ac.TestActivity().listlottyName(env, activityId, sessionIdlist[1], 2).json()['data'])
        num1=ac.TestActivity().listlottyName(env, activityId, sessionIdlist[0], 2).json()['data']['total']
        num2=ac.TestActivity().listlottyName(env, activityId, sessionIdlist[1], 2).json()['data']['total']
        if num1==1:
            pytest.assume(num1==1)
            pytest.assume(num2==0)

    @allure.testcase('微信会员，openid消耗积分报名')
    def test_memberopenid(self,env,branchId):
        activityId = getconf(env, game)
        delsession(env, activityId)
        #取第3条数据；1级奖
        n,a=3,0
        realopen_id = ac.TestUser().getmemsql(env,branchId)[0]
        # 给会员加积分
        sql='  SELECT card_code	FROM  buorg.org_member  WHERE openid="' + realopen_id + '" LIMIT 1'
        card_code = OperationDb(env).select_one(sql)
        ac.TestUser().increasepoint(env,100,card_code)
        # 创建场次抽奖
        startTime, prossingStartTime, endTime = gettime(40), gettime(70), gettime(90)
        ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        sessionIdlist=choujiang(env,activityId,n,a,"openId",realopen_id,3)
        time.sleep(10)
        x=ac.TestActivity().listlottyName(env, activityId, sessionIdlist[0], 2).json()
        print (x)
        if x['data']['pages']==0:
            print ("openid会员没中奖")
        else:
            # print ("openid会员奖",x['data'])
            pytest.assume(x['data']['total']==1)

    @allure.testcase('手机号会员，openid消耗积分报名')
    # 会员/wechat，前端输入，openid关键字，手工录入
    def test_memberphone(self, env,branchId):
        activityId = getconf(env, game)
        delsession(env, activityId)
        # 取第4条数据；1级奖
        n, a = 4, 0
        realopen_id = ac.TestUser().getmemsql(env,branchId)[0]
        print ("手机会员openid",realopen_id)
        # 给会员加积分
        sql='  SELECT card_code	FROM  buorg.org_member  WHERE openid="' + realopen_id + '" LIMIT 1'
        card_code = OperationDb(env).select_one(sql)
        ac.TestUser().increasepoint(env,100,card_code)
        # 创建场次抽奖
        startTime, prossingStartTime, endTime = gettime(40), gettime(60), gettime(80)
        ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        sessionIdlist = choujiang(env, activityId, n, a, "openId", realopen_id, 3)
        time.sleep(10)
        # print ("手机号会员奖请求时间",gettime(0))
        x =ac.TestActivity().listlottyName(env, activityId, sessionIdlist[0], 2).json()
        if x['data']['pages']==0:
            print ("手机号会员没中奖")
        else:
            # print("手机号会员奖",x['data'])
            pytest.assume(x['data']['total'] == 1)

    @allure.testcase('独立奖池，1,2级奖，手动开奖，单场中奖限制1')
    # （非会员 / 白名单，前端输入，手机号关键字，手工录入）
    def test_twolevprizelimit(self,env):
        activityId = getconf(env, game)
        delsession(env, activityId)
        phone = getphone()
        #取第二条数据；1,2级奖
        n,a=2,1
        # 新增场次
        startTime, prossingStartTime, endTime = gettime(40), gettime(60), gettime(80)
        startTime1, prossingStartTime1, endTime1 = gettime(81), gettime(101), gettime(121)
        ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        ac.TestSession().saveSession(env, activityId, startTime1, prossingStartTime1, endTime1)
        sessionIdlist = choujiang(env, activityId, n, a, "phone", phone,5)
        # time.sleep(1)
        print("无限制第一次中奖", ac.TestActivity().listlottyName(env, activityId, sessionIdlist[0], 2).json()['data'])
        time.sleep(10)
        print("无限制第二次中奖", ac.TestActivity().listlottyName(env, activityId, sessionIdlist[1], 2).json()['data'])
        pytest.assume(ac.TestActivity().listlottyName(env, activityId, sessionIdlist[0], 2).json()['data']['total']==1)
        pytest.assume(ac.TestActivity().listlottyName(env, activityId, sessionIdlist[1], 2).json()['data']['total']==1)

    @allure.testcase('白名单，后台导入,手机号关键字，手动发奖')
    def Whitelisthoutai(self, env):
        activityId = getconf(env, game)
        delsession(env, activityId)
        # 取第5条数据；1级奖
        n, a = 5, 0
        phone=getphone()
        openid = getopenId()
        userid=str(random.randint(10000, 99999))
        updatefile(2,4, phone,'pwl')
        updatefile(2,5, openid,'pwl')
        updatefile(2,6, userid,'pwl')
        print("手机号关键字的用户参数", phone, openid, userid)
        # 创建场次
        startTime, prossingStartTime, endTime = gettime(40), gettime(50), gettime(70)
        ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        # 不需要报名，手动发奖
        sessionIdlist = choujiang(env, activityId, n, a, "phone", phone, 5)
        time.sleep(35)
        print("手机号关键字查奖时间", gettime(0))
        print("手机号关键字打印场次", sessionIdlist)
        print("手机号关键字中奖",ac.TestActivity().listlottyName(env, activityId, sessionIdlist[0], 2).json()['data'])
        # pytest.assume(
            # ac.TestActivity().listlottyName(env, activityId, sessionIdlist[0], 2).json()['data']['total'] == 1)

    @allure.testcase('白名单，后台导入,openid关键字，自动发奖，2级奖')
    def test_openidautoprize(self, env):
        activityId = getconf(env, game)
        delsession(env, activityId)
        delwhitelist(env, activityId)
        # 取第6条数据；2级奖
        data = getdata(env, game, 6)
        ac.TestActivity().updateactivity(env, data)
        ac.TestActivity().enableOrDisableGame(env, activityId, 1)
        phone=getphone()
        openid = getopenId()
        userid=str(random.randint(10000, 99999))
        updatefile(2,4, phone,'pwl')
        updatefile(2,5, openid,'pwl')
        updatefile(2,6, userid,'pwl')
        print ("openid关键字的用户参数",phone,openid,userid)
        ac.TestActivity().uploadFile(env, activityId)
        # 创建场次
        startTime, prossingStartTime, endTime = gettime(1), gettime(2), gettime(3)
        ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        time.sleep(15)
        print("openid关键字查奖时间", gettime(0))
        # 不需要报名，自动发奖
        sessionId=ac.TestSession().sessionlist(env, activityId).json()['data'][0]['id']
        print("openid关键字打印场次", sessionId)
        print("openid关键字自动发奖",ac.TestActivity().listlottyName(env, activityId, sessionId, 2).json()['data'])
        pytest.assume(ac.TestActivity().listlottyName(env, activityId, sessionId, 2).json()['data']['total'] == 1)

    @allure.testcase('白名单，后台导入,userid关键字，自动发奖，1级奖')
    def test_userid(self, env):
        activityId = getconf(env, game)
        delsession(env, activityId)
        delwhitelist(env, activityId)
        # 取第7条数据；上传白名单
        data = getdata(env, game, 7)
        ac.TestActivity().updateactivity(env, data)
        ac.TestActivity().enableOrDisableGame(env, activityId, 1)
        phone=getphone()
        openid = getopenId()
        userid=str(random.randint(10000, 99999))
        updatefile(2,4, phone,'pwl')
        updatefile(2,5, openid,'pwl')
        updatefile(2,6, userid,'pwl')
        print("userid关键字的用户参数", phone, openid, userid)
        ac.TestActivity().uploadFile(env, activityId)
        # 创建场次
        startTime, prossingStartTime, endTime = gettime(1), gettime(2), gettime(3)
        ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        # 不需要报名，自动发奖
        time.sleep(15)
        sessionId=ac.TestSession().sessionlist(env, activityId).json()['data'][0]['id']
        print("userid关键字打印场次", sessionId)
        print("userid关键字自动发奖",ac.TestActivity().listlottyName(env, activityId, sessionId, 2).json()['data'])
        pytest.assume(ac.TestActivity().listlottyName(env, activityId, sessionId, 2).json()['data']['total']== 1)

    def danmu(self,env):
        # activityId = '444'
        activityId = '180'
        delsession(env, activityId)
        startTime1, prossingStartTime1, endTime1 = gettime(0), gettime(100), gettime(160)
        yyy=ac.TestSession().saveSession(env, activityId, startTime1, prossingStartTime1, endTime1)
        startTime, prossingStartTime, endTime = gettime(170), gettime(260), gettime(300)
        ac.TestSession().saveSession(env, activityId, startTime, prossingStartTime, endTime)
        print(yyy.json())
        #获取当前有效场次
        r1=ac.TestSession().getEffective(env,activityId)
        id=int(r1.json()['data']['id'])
        stubId = str(random.randint(10000, 99999))
        # for i in range(10):
        #     # realopen_id = 'o7rXxv2ZzkhcZSRc0f3-ngquFOKI'
        #     realopen_id = 'olH54xEJQ0PctWM6w_uk8kaFNhi0'
        #     # realopen_id = 'olH54xCc2B-2IwIT34EeJB9EtOrg'
        #     # realopen_id = 'o7rXxv_muG5v45bfKbJnjq5kenSU'
        #     # 给会员加积分
        #     # sql='SELECT card_code	FROM  buorg.org_member  WHERE openid="' + realopen_id + '" LIMIT 1'
        #     # card_code = OperationDb(env).select_one(sql)
        #     # card_code = 'D7AC835fDe6110779A61D0A18933294A'
        #     # xxxx=ac.TestUser().increasepoint(env,100,card_code)
        #     # print ("加积分",xxxx.json())
        #     r3=ac.TestUser().signUp(env,activityId,id,"openId",realopen_id,3,1,stubId,0)
        #     print (r3.json())
        #     time.sleep(2)

if __name__ == '__main__':
    # test_noSession,test_unstartSession，test_openidautoprize,test_userid
    # test_Session，test_beforeSession,test_twolevprizelimit,test_onelevprizelimit,Whitelisthoutai
    # command='pytest -s -v --env=test  jinlibang.py::Test_jinlibang'
    command='pytest -s --env=v4 --branchId=174 jinlibang.py::Test_jinlibang'
    os.system(command)
    # choujiang("v4", "37", 7, 0, "userId", 77777, 5)
    # Test_jinlibang().test_userid("v4")
    # delwhitelist("v4", 37)
    # Test_jinlibang().danmu("v4")